package cn.yj.core.business.controller;

import cn.yj.annotation.pagehelper.page.PageUtils;
import cn.yj.cgs.dba.entity.Customer;
import cn.yj.common.Enums;
import cn.yj.common.ImageUtils;
import cn.yj.commons.utils.StringUtils;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.commons.utils.MapUtils;
import cn.yj.config.annotation.Anonymous;
import cn.yj.config.web.AppBaseController;
import cn.yj.core.business.service.CustomerService;
import cn.yj.entity.R;
import com.google.zxing.WriterException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-04-20 16:58
 */
@RestController
@RequestMapping("/customer")
public class CustomerController extends AppBaseController<Customer> {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);

    @Resource
    private CustomerService customerService;

    /**
     * 用户信息更新
     *
     * @param customer 用户对象
     */
    @OperateLog(describe = "更新用户信息", type = LogType.UPDATE)
    @PostMapping("/update")
    public R update(@RequestBody Customer customer) {
        customer.setUserCode(getUserCode());
        return result(customerService.updateByUserCode(customer));
    }

    /**
     * 邀请有礼的排名列表
     */
    @GetMapping("/rankingList")
    @Anonymous
    public R rankingList() {
        Map<String, Object> map = getMap("page", customerService.inviteesRankingList(PageUtils.page(),
                new HashMap<>()));
        map.put("effectiveCount", Enums.Common.ZERO);
        map.put("giftTip", "");
        return success(map);
    }

    /**
     * 获取我的排名列表
     */
    @GetMapping("/myRanking")
    public R myRanking() {
        return success(customerService.getMyRanking(getUserCode()));
    }

    /**
     * 获取我邀请的用户
     */
    @GetMapping("/getMyCustomer")
    public R getMyCustomer() {
        Map<String, Object> map = new HashMap<>();
        map.put("page", customerService.myInvitees(PageUtils.page(), getUserCode()));
        long count = customerService.countEffectiveCustomer(getUserCode());
        map.put("effectiveCount", count);
        String giftTip = "";
        if (count >= 500) {
            giftTip = "恭喜同学获得 [智能手机一部]";
        } else if (count >= 200) {
            giftTip = "恭喜同学获得 [小米智能手环] 一个";
        } else if (count >= 100) {
            giftTip = "恭喜同学获得一副 [aipos2/其它蓝牙耳机]";
        } else if (count >= 50) {
            giftTip = "恭喜同学获得 [USB/阅读灯/小风扇] 一个";
        } else if (count >= 15) {
            giftTip = "恭喜同学获得 [马克杯] 一个";
        }
        map.put("giftTip", giftTip);
        return success(map);
    }

    @OperateLog(describe = "头像更新", type = LogType.UPDATE)
    @PostMapping("/updateAvatar")
    public R updateAvatar(@RequestBody Map<String, Object> body) {
        MapUtils.validateKeyValueIsEmpty(body, new String[]{"avatar"});
        String avatar = MapUtils.getStringVal(body, "avatar");
        return success(customerService.updateAvatar(avatar, getUserCode()));
    }


    @GetMapping("/getInviteCode")
    public R getInviteCode() {
        return success(customerService.getInviteQrCode(getUserCode()));
    }

    @GetMapping("/getMyInfo")
    public R getMyInfo() {
        return success(customerService.getCustomVoByMiniOpenId(getOpenId()));
    }

    @GetMapping("/checkAuth")
    public R checkAuth() {
        return result(customerService.authenticated(getUserCode()));
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/userConcatInfo/{userCode}")
    @Anonymous
    public R userConcatInfo(@PathVariable String userCode) {
        return success(customerService.getConcatInfoByCode(userCode));
    }


    /**
     * 更新邀请码
     *
     * @param params - invitees
     */
    @PostMapping("/updateInvitees")
    public R updateInvitees(@RequestBody Map<String, Object> params) {
        return success(customerService.updateInvitees(getUserCode(), params));
    }

    /**
     * 更新用户所在的城市
     *
     * @param body 信息
     */
    @OperateLog(describe = "更新城市")
    @PostMapping("/updateUserCity")
    public R update(@RequestBody Map<String, Object> body) {
        Customer customer = new Customer();
        String[] keys = {"city"};
        MapUtils.validateKeyValueIsEmpty(body, keys);
        customer.setCity(body.get("city").toString());
        customer.setUserCode(getUserCode());
        return result(customerService.updateCityByUserCode(customer));
    }


    @PostMapping("/createPosters")
    public R createPosters() throws IOException, WriterException {
        String myInviteCode = getMyInviteCode();
        String userCode = getUserCode();
        LOGGER.info("创建列表海报邀请码={}，用户编号={}", myInviteCode, userCode);
        return success(ImageUtils.addInviteCodeMark(myInviteCode));
    }

}
